package com.sgcc.pda.hzaidl.impl;


import android.os.RemoteException;
import android.text.TextUtils;

import com.sgcc.pda.gis.LocationResult;
import com.sgcc.pda.hw.ReturnResult;
import com.sgcc.pda.hzaidl.AIDLServiceImpl;
import com.sgcc.pda.hzaidl.IHZAidl;
import com.sgcc.pda.hzaidl.IHZAidlCallback;
import com.sgcc.pda.hzaidl.entity.GisCallback;
import com.sgcc.pda.hzaidl.entity.HardWareCallback;
import com.sgcc.pda.hzaidl.entity.RequestBean;
import com.sgcc.pda.hzaidl.entity.ResponseBean;
import com.sgcc.pda.tools.JsonUtils;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.pda.util.PlatConstant;
import com.sgcc.platforminterface.PlatfInterfManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建者：suven on 2019/12/16 17:25
 * @描述：
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public class GisUtil {

    private static IHZAidl mHZAidl;

    private static AIDLServiceImpl getHardWareManager() {
        return AIDLServiceImpl.getInstance();
    }
    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     */
    public static String requestGisString(String methodName) {
        return requestGisString(methodName, "");
    }

    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     * @param paramJson  参数列表
     */
    public static String requestGisString(String methodName, String paramJson) {
        RequestBean requestBean = getGis(methodName, paramJson);
        String result = getHardWareManager().post(requestBean.toString(), null);
        ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
        if (responseBean != null && responseBean.getRetCode() == 1) {
            return responseBean.getMsgJson();
        } else {
            return "";
        }
    }

    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     */
    public static LocationResult requestGisData(String methodName) {
        return requestGisData(methodName, "");
    }

    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     * @param paramJson  参数列表
     */
    public static LocationResult requestGisData(String methodName, String paramJson) {
        RequestBean requestBean = getGis(methodName, paramJson);
        String result = getHardWareManager().post(requestBean.toString(), null);
        ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
        if (responseBean != null && responseBean.getRetCode() == 1) {
            return JsonUtils.toBean(responseBean.getMsgJson(), LocationResult.class);
        }
        return null;
    }

    /**
     * 无返回值
     *
     * @param methodName 方法名
     * @param paramJson   参数
     */
    public static void requestGisNoData(String methodName, String paramJson) {
        requestgetGisData(methodName, paramJson, null);
    }


    /**
     * 硬件服务方法，带回调返回值
     *
     * @param methodName 方法名
     * @param callback   回调
     */
    public static void requestGisData(String methodName, final GisCallback callback) {
        requestgetGisData(methodName, "", callback);
    }

    /**
     * 硬件服务方法，带回调返回值
     *
     * @param methodName 方法名
     * @param paramJson  参数
     * @param callback   回调
     * @
     */
    public static void requestgetGisData(String methodName, String paramJson, final GisCallback callback) {
        RequestBean requestBean = getGis(methodName, paramJson);
        getHardWareManager().post(requestBean.toString(), new IHZAidlCallback.Stub() {
            @Override
            public void callback(String result) throws RemoteException {
                try {
                    if (TextUtils.isEmpty(result)) {
                        callback.onReceiverData(null);
                        return;
                    }
                    ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
                    if (responseBean != null) {
                        if (responseBean.getRetCode() == 1) {
                            LocationResult returnResult = JsonUtils.toBean(responseBean.getMsgJson(), LocationResult.class);
                            callback.onReceiverData(returnResult);
                        } else {
                            callback.onReceiverData(null);
                        }
                    } else {
                        callback.onReceiverData(null);
                    }
                } catch (Exception e) {
                    LogPrintUtils.d("requestHardwareData:", e);
                    callback.onReceiverData(null);
                }
            }
        });
    }

    /**
     * Gis服务
     *
     * @param methodName 方法名
     * @param paramJson
     * @param callback   结果回调
     */
    public static void requestGisData(String methodName, String paramJson, IHZAidlCallback.Stub callback) {
        RequestBean requestBean = getGis(methodName, paramJson);
        getHardWareManager().post(requestBean.toString(), callback);
    }


    /**
     * Gis服务
     *
     * @param methodName 方法名
     * @param paramJson  参数
     * @return 返回请求对像
     */
    private static RequestBean getGis(String methodName, String paramJson) {
        return new RequestBean(PlatConstant.IWebCmd.GIS, methodName, paramJson, PlatfInterfManager.getInstance().getPackageName(), "");
    }
}
